package sanrenxing.dayEnd.backRepertoryTotal.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import sanrenxing.dayEnd.backRepertoryTotal.domain.users;
import sanrenxing.dayEnd.backRepertoryTotal.service.userbiz;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/uc")
public class usersController  {
    @Autowired
    private sanrenxing.dayEnd.backRepertoryTotal.utils.sdf sdf;
    @Resource
    private userbiz ub;
    @PostMapping("/signin")
    @ResponseBody
    public String signin(@RequestBody users us, HttpServletRequest re) throws ParseException {
        HttpSession hs=re.getSession();
        users sn=ub.one(us);
        if(sn!=null){
            hs.setAttribute("sn",sn);
            return "main";
        }
        return "sb";
    }
    @GetMapping("/us")
    @ResponseBody
    public Map us(int limit,int page, HttpServletRequest re){
        users su=new users();
        if(limit==0){
            limit=5;
        }
        if(page==0){
            page=1;
        }
        System.out.println(limit);
        System.out.println(page);
        if(re.getParameter("userId")!=null&&re.getParameter("userId").trim().length()>0){
   su.setUserId(Integer.valueOf(re.getParameter("userId")));
        }
        if(re.getParameter("userName")!=null&&re.getParameter("userName").trim().length()>0){
su.setUserName(re.getParameter("userName"));
        }
        if(re.getParameter("userState")!=null&&re.getParameter("userState").trim().length()>0){
   su.setUserState(Integer.valueOf(re.getParameter("userState")));
        }
        String xx=ub.xx(su);

        Map<String,Object> ma=new HashMap<>();
        ma.put("code",0);
        ma.put("msg","");
        ma.put("count",ub.count(xx));
        ma.put("data",ub.all(xx,limit,page));
        ma.put("xl",ub.rfal());
        return ma;
    }
    @PostMapping("/us")
    @ResponseBody
    public String add(@RequestBody users us){
        return ub.add(us)>0?"cg":"sb";
    }
    @PutMapping("/us")
    @ResponseBody
    public String xg(@RequestBody users us){
        return ub.update(us)>0?"cg":"sb";
    }
    @DeleteMapping("/us/{id}")
    @ResponseBody
    public String xg(@PathVariable("id") int us){
        return ub.del(us)>0?"cg":"sb";
    }
    @PutMapping("/uss/{ss}")
    @ResponseBody
    public String dels(@PathVariable("ss") String ss){
        return ub.updates(ss)>0?"cg":"sb";
    }

    /**
     * 这是修改密码的方法
     * @param newPassword
     * @param oldPassword
     * @return
     */
    @PutMapping("/method/{newPassword}/{oldPassword}")
    @ResponseBody
    public int updatePassword(@PathVariable("newPassword")String newPassword, @PathVariable("oldPassword")String oldPassword, HttpServletRequest request){
        return ub.updatePassword(newPassword, oldPassword, request);
    }

    @PostMapping("/method/{userId}")
    @ResponseBody
    public void resetPwd(@PathVariable("userId")String userId){
        ub.resetPwd(userId);
    }

    @RequestMapping("/warning")
    @ResponseBody
    public String warning(){
        return "休假日不可以进行增加、删除、修改操作";
    }
}
